/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JplManagement;

import java.util.ArrayList;
import jpl.*;
import parserjson.*;

/**
 *
 * @author Lore
 */
public class useJpl {

    public ElementosWumpus boardW;
    public String JugX;
    public String JugY;

    public Query callQuery() {
        Query q1 =
                new Query(
                "consult",
                new Term[]{new Atom("C:\\Users\\Lore\\Desktop\\test.pl")});

        return q1;
    }

    public Query callQuery(String path) {
        Query q1 =
                new Query(
                "consult",
                new Term[]{new Atom(path)});

        return q1;
    }

    public void initializeWumpus(problem tablero, String filaJug, String colJug) {

        if (tablero == null) {
            return;
        }

        useJpl u = new useJpl();
        Query q = u.callQuery("files\\mundoWumpus.pl");

        q.query();
        int wumpusx = 1, wumpusy = 1;

        wumpusx = tablero.wumpus.row;
        wumpusy = tablero.wumpus.col;

        System.out.println("Wumpus: [" + wumpusx + "," + wumpusy + "]");
        System.out.println("Oro: [" + tablero.gold.row + "," + tablero.gold.col + "]");
        System.out.println("Pit1: [" + tablero.pits[0].row + "," + tablero.pits[0].col + "]");
        System.out.println("Pit2: [" + tablero.pits[1].row + "," + tablero.pits[1].col + "]");
        System.out.println("Pit3: [" + tablero.pits[2].row + "," + tablero.pits[2].col + "]");

        Query q4 = null;
        try {
            q4 = new Query("retractall(land_extent(_)),"
                    + "retractall(wumpus_location(_)),"
                    + "retractall(wumpus_healthy),"
                    + "retractall(gold_location(_)),"
                    + "retractall(pit_location(_)),"
                    + "assert(land_extent(5)),"
                    + "assert(wumpus_healthy),"
                    + "retractall(agent_location(_)),"
                    + "retractall(direccion_agente(_)),"
                    + "retractall(agent_healthy),"
                    + "retractall(agent_hold),"
                    + "retractall(agent_arrows(_)),"
                    + "retractall(agent_goal(_)),"
                    + "retractall(agent_score(_)),"
                    + "retractall(is_wumpus(_,_)),"
                    + "retractall(is_pit(_,_)),"
                    + "retractall(is_gold(_)),"
                    + "retractall(is_wall(_)),"
                    + "retractall(is_dead),"
                    + "retractall(is_visited(_)),"
                    + "assert(agent_location([" + filaJug + "," + colJug + "])),"
                    + "assert(direccion_agente(0)),"
                    + "assert(agent_healthy),"
                    + "assert(agent_arrows(1)),"
                    + "assert(agent_goal(find_out)),"
                    + "assert(agent_score(0)),"
                    + "assert(agent_in_cave),"
                    + "retractall(time(_)),"
                    + "assert(time(0)),"
                    + "retractall(nb_visited(_)),"
                    + "assert(nb_visited(0)),"
                    + "retractall(score_agent_dead(_)),"
                    + "assert(score_agent_dead(10000)),"
                    + "retractall(score_climb_with_gold(_)),"
                    + "assert(score_climb_with_gold(1000)),"
                    + "retractall(score_grab(_)),"
                    + "assert(score_grab(0)),"
                    + "retractall(score_wumpus_dead(_)),"
                    + "assert(score_wumpus_dead(0)),"
                    + "retractall(is_situation(_,_,_,_,_)),"
                    + "retractall(short_goal(_)),"
                    + "agent_healthy_state(AFG),"
                    + "wumpus_healthy_state(WFS),"
                    + "time(T),"
                    + "nb_visited(N),"
                    + "land_extent(LE),"
                    + "E is (LE - 1) * (LE - 1),"
                    + "agent_score(SC),"
                    + "agent_hold_state(AHS),"
                    + "agent_goal(G),"
                    + "agent_in_the_cave_state(ACS),"
                    + "retractall(is_situation(_,_,_,_,_)),"
                    + "time(T),agent_location(L),direccion_agente(O),"
                    + "assert(is_situation(T,L,O,[],i_know_nothing)),"
                    + "agent_healthy"
                    + ".");



            while (q4.hasMoreSolutions()) {
                java.util.Hashtable solution;
                solution = q4.nextSolution();

            }
        } catch (PrologException pl) {

            System.out.println(pl.getMessage());
        }


    }

    public ArrayList getWumpusStep(ArrayList perception) {
        ArrayList orientacion_accion = new ArrayList();

        Query q4 = null, q5 = null, q6 = null, q7 = null;

        Query qPosactual = null;
        String action = "";

        String Olor = Boolean.parseBoolean(perception.get(0).toString()) ? "yes" : "no";
        String Brisa = Boolean.parseBoolean(perception.get(1).toString()) ? "yes" : "no";
        String Brillo = Boolean.parseBoolean(perception.get(2).toString()) ? "yes" : "no";
        String Grito = Boolean.parseBoolean(perception.get(4).toString()) ? "yes" : "no";
        String Pared = Boolean.parseBoolean(perception.get(5).toString()) ? "yes" : "no";

        if (Boolean.parseBoolean(perception.get(3).toString())) {
            try {
                q4 = new Query("retractall(agent_healthy), retractall(agent_location(_)).");
                while (q4.hasMoreSolutions()) {
                    java.util.Hashtable solution;
                    solution = q4.nextSolution();

                }
                orientacion_accion.add("-1");
                orientacion_accion.add("fin");
            } catch (PrologException p) {
            }

        } else {

            try {
                q5 = new Query("agent_healthy,"
                        + "agent_in_cave,"
                        + "is_nb_visited,"
                        + "agent_location(L),"
                        + "retractall(is_visited(L)),"
                        + "assert(is_visited(L)).");
                q5.query();


                q7 = new Query("tell_KB([" + Olor + ", " + Brisa + ", " + Brillo + ", " + Pared + ", " + Grito + "]),"
                        + "ask_KB(Action),"
                        + "apply(Action),"
                        + "short_goal(SG),"
                        + "time(T),"
                        + "New_T is T+1,"
                        + "retractall(time(_)),"
                        + "assert(time(New_T)),"
                        + "direccion_agente(O),"
                        + "assert(is_situation(New_T,L,O,[" + Olor + ", " + Brisa + ", " + Brillo + ", " + Pared + ", " + Grito + "],SG)),"
                        + "agent_location(X),"
                        + "!.");

                while (q7.hasMoreSolutions()) {
                    java.util.Hashtable solution;
                    solution = q7.nextSolution();

                    action = solution.get("Action").toString();

                    orientacion_accion.add(solution.get("O"));
                    orientacion_accion.add(solution.get("Action"));

                }

            } catch (PrologException pl) {
            }
        }

        return orientacion_accion;
    }
}
